//
//  FCPGridTemplate.swift
//  flutter_carplay
//
//  Created by Oğuzhan Atalay on 21.08.2021.
//

import CarPlay

@available(iOS 14.0, *)
class FCPGridTemplate: BaseTabBarTemplate{
  private(set) var _super: CPGridTemplate?
  var elementId: String!
  var title: String!
  var systemIcon: String?
  var showsTabBadge: Bool! = false
  private var buttons: [CPGridButton]
  private var objcButtons: [FCPGridButton]
  
  required init(obj: [String : Any]) {
    self.elementId = obj["_elementId"] as? String
    self.title = obj["title"] as? String
    self.systemIcon = obj["systemIcon"] as? String
    self.showsTabBadge = obj["showsTabBadge"] as? Bool  ?? false
    self.objcButtons = (obj["buttons"] as! Array<[String : Any]>).map {
      FCPGridButton(obj: $0)
    }
    self.buttons = self.objcButtons.map {
      $0.get
    }
  }
  
  var get: CPTemplate {
    let gridTemplate = CPGridTemplate.init(title: self.title, gridButtons: self.buttons)
    gridTemplate.showsTabBadge = showsTabBadge
    if systemIcon != nil {
      gridTemplate.tabImage = UIImage(systemName: systemIcon!)
    }
    self._super = gridTemplate
    return gridTemplate
  }
}

@available(iOS 14.0, *)
extension FCPGridTemplate: FCPRootTemplate { }
